package com.liaoyifan.core.opera;

import com.liaoyifan.core.util.RequestContext;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import lombok.extern.log4j.Log4j2;

@SuppressWarnings("unused")
@Log4j2
public class FeignTokenInterceptor implements RequestInterceptor {

    private static final String USER_AGENT = "User-Agent";
    private final String applicationName;

    public FeignTokenInterceptor(String applicationName) {
        this.applicationName = applicationName;
    }

    @Override
    public void apply(RequestTemplate request) {
        request.header(
                RequestContext.AUTHORIZATION,
                RequestContext.BEARER + RequestContext.getAuthorization());
        request.header(RequestContext.X_TRACE, RequestContext.getTrace());
        request.header(USER_AGENT, this.applicationName);
        log.info("Feign headers: {}", request.headers().toString());
    }
}
